﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FytSoa.Core.Model.Sw;
using FytSoa.Extensions;
using FytSoa.Service.DtoModel;
using FytSoa.Service.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FytSoa.Api.Controllers.Shop
{
    [Route("api/shop/[controller]")]
    [ApiController]
    [JwtAuthorize(Roles = "Admin")]
    public class ActivityController : ControllerBase
    {
        private readonly ISw_activityService _activityService;
        private readonly ISw_activitygoodService _acgoodsService;
        public ActivityController(ISw_activityService activityService
            , ISw_activitygoodService acgoodsService)
        {
            _activityService = activityService;
            _acgoodsService = acgoodsService;
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet("goods/list")]
        public async Task<IActionResult> GetActivityGoodsPage([FromQuery]PageParm param)
        {
            return Ok(await _acgoodsService.GetPageGoods(param));
        }

        /// <summary>
        /// 关联商品/取消关联
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("linkgoods")]
        public async Task<IActionResult> ActivityLinkGoods([FromBody]Sw_activitygood model)
        {
            return Ok(await _acgoodsService.ActivityLinkGoods(model));
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet("list")]
        public async Task<IActionResult> GetActivityPage([FromQuery]PageParm param)
        {
            return Ok(await _activityService.GetPagesAsync(param, m => true, m => m.AddTime, Common.DbOrderEnum.Desc));
        }

        /// <summary>
        /// 根据id获得信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("getmodel")]
        public async Task<IActionResult> GetModel([FromBody]ParmString obj)
        {
            return Ok (await _activityService.GetModelAsync(m => m.Guid == obj.parm));
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<IActionResult> Add([FromBody]Sw_activity model)
        {
            model.Guid = Guid.NewGuid().ToString();
            return Ok(await _activityService.AddAsync(model));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("update")]
        public async Task<IActionResult> Update([FromBody]Sw_activity model)
        {
            return Ok(await _activityService.UpdateAsync(model));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task<IActionResult> Delete([FromBody]ParmString obj)
        {
            return Ok(await _activityService.DeleteAsync(obj.parm));
        }
    }
}